package com.zl.easybuy.web;

import com.zl.easybuy.pojo.User;
import com.zl.easybuy.service.UserService;
import com.zl.easybuy.util.Constan;
import com.zl.easybuy.util.SessionUtils;
import com.zl.easybuy.vo.ResultCode;
import com.zl.easybuy.vo.ResultVO;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping("/queryById") //访问路径
    public String queryById(Integer id, Model model) {
        User user = userService.queryById(id);
        model.addAttribute("user", user);
        return "forward:/index.jsp"; //转发到index.jsp
    }

    /**
     * 登录接口
     */
    @RequestMapping("/Login")
    //表示这个方法的返回值以数据形式响应给前端，如果添加的JSON相关依赖，把java对象转换为JSON字符串响应
    @ResponseBody
    public ResultVO<Null> login(String loginName,String password){
        return  userService.login(loginName,password)? ResultVO.success():ResultVO.returnFail(ResultCode.LOGIN_FAIL);
    }

    //将页面名称传入进行重定向跳转
    @RequestMapping("/page/{pageName}")
    public  String jumpPage(@PathVariable("pageName")String page){
        return "redirect:/pre/"+page+".jsp"; //重定向到某个页面
    }
    //注销
    @RequestMapping("/loginOut")
    public String loginout(){
        //让session失效
        SessionUtils.invalidSession();
        //重定向到login.jsp页面
        return "redirect:/pre/login.jsp";
    }
    //注册
    @PostMapping("/Register")
    @ResponseBody
    public ResultVO<Null> register(User user){
        //设置用户类型:
        user.setType(Constan.USER_TYPE_NOMAL);
        return userService.addUser(user)?ResultVO.success():ResultVO.returnFail(ResultCode.REGISTER_FAIL);
    }
}
